<template>
	<view>
		<view class="top_view flex">
			<view class="top_view_item"
				v-for="(item,index) in tap_list"
				:key="index"
				@click="choose_tap(index)">
				<image :src=" tap_index == index?item.img_active :item.img "
				class="top_view_item_img" mode="" />
				<view :class="['top_view_item_text',tap_index == index?'tap_active' :'']">
					{{item.title}}
				</view>
			</view>
		</view>
		<scroll-view scroll-y="true" class="scroll" :style="{height:scrollH+'px'}">
			<view class="list" v-if="type != 4">
				<view class="list_item" @click="goDetail(item)" v-for="(item,index) in list" :key="index">
					<view class="list_item_time">
						{{item.created_at}}
					</view>
					<view class="list_item_content">
						<view class="list_item_content_top">
							{{item.title}}
						</view>
						<view class="list_item_content_min">
							<!-- {{item.content}} -->
							{{item.content_text}}
						</view>
						<view class="list_item_content_bottom flex1">
							<image src="https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg_icon.png" class="messg_icon" mode=""></image>
							<view class="flex1name">
								线上名医工作室
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="list_ys"
				v-if="type == 4">
				<view class="list_ys_item flex"
					v-for="(item,index) in list"
					:key="index"
					@click="go_im(item)">
					<view class="list_ys_item_left">
						<image :src="ImgUrl + item.avatar" class="list_ys_item_img" mode=""></image>
						<view v-if="item.unread_num" class="num">
							{{item.unread_num}}
						</view>
					</view>
					<view class="list_ys_item_right">
						<view class="list_ys_item_right_top">
							{{item.name}}
						</view>
						<view :style="item.msg?'':'opacity: 0;'" class="list_ys_item_right_bottom line-1">
							{{ item.msg?item.msg:'啊' }}
						</view>
					</view>
				</view>
			</view>
			<view class="epty" v-if="list.length == 0">
				<view class="epty_zhanwei"></view>
				<image src="https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg_epty.png" class="epty_img" mode=""></image>
				<view class="epty_text">
					暂无消息通知～
				</view>
			</view>
			
		</scroll-view>
	</view>
</template>

<script>
	import { message } from '../../api/common.js';
	import getScrollHeight from "@/utils/utils.js"
	export default {
		data() {
			return {
				type: 4,
				scrollH:'',
				tap_list:[
					{
						title:'系统消息',
						img:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg1_no.png',
						img_active:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg1.png'
					},
					{
						title:'支付消息',
						img:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg2_no.png',
						img_active:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg2.png'
					},
					{
						title:'平台消息',
						img:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg3_no.png',
						img_active:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg3.png'
					},
					{
						title:'医生消息',
						img:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg4_no.png',
						img_active:'https://bucketoss10086.oss-cn-beijing.aliyuncs.com/images/messg4.png'
					}
				],
				tap_index:3,
				list:[],
			}
		},
		computed:{
			conversationList(){
				return this.$store.state.conversationList
			},
			ImgUrl(){
				return this.$store.state.ImgUrl
			},
		},
		watch: {
			conversationList(newValue, oldValue) {
				// console.log(newValue, 'cs-cs');
				// if(this.list.length!=0||this.type==4){
				// 	this.setUnReadNum(newValue);
				// }
				// let timer = setTimeout(res=>{
					
				// },1000)
				if(this.type==4){
					this.getList();
				}
			}
		},
		onReady() {
			let t = this
			getScrollHeight.getScrollHeight('scroll').then(res=>{
				t.scrollH = res-10
			})
		},
		mounted() {
			// this.getList();
		},
		onShow() {
			this.getList();
		},
		methods: {
			goDetail(item){
				uni.setStorageSync('detail',item);
				uni.navigateTo({
					url:'./message2'
				})
			},
			getList(){
				message({type: this.type}).then(res=>{
				// message().then(res=>{
					if(res.code == 200001){
						if(this.type!=4){
							this.list = res.data;
						} else {
							this.list = res.data.data
							// if(this.list.length!=0&&this.conversationList.length!=0){
							// 	this.setUnReadNum(this.conversationList);
							// }
						}
					}
				})
			},
			choose_tap(index){
				this.tap_index = index;
				this.type = Number(index) + 1
				this.getList();
			},
			go_im(item){
				uni.navigateTo({
					url:'/pages/IM/IM?id='+item.id+'&group_id='+item.group_id
				})
			},
			// 设置列表未读书
			setUnReadNum(list){
				let list2 = this.list;
				for(let i=0,len=list.length;i<len;i++){
					for(let j=0,lens=list2.length;j<lens;j++){
						if(list[i].conversationID.replace(list[i].type,'')==list2[j].group_id){
							this.list[j].unreadCount = list[i].unreadCount;
							if(list[i].lastMessage.type == 'TIMCustomElem'){
								this.list[j].msg = '[图片]'
							}else if(list[i].lastMessage.type == 'TIMTextElem'){
								this.list[j].msg = list[i].lastMessage.payload.text;
							}
						}
					}
				}
				// console.log(list);
				// console.log(this.list);
				this.$forceUpdate();
			}
		}
	}
</script>

<style>
	page{
		background: #fff;
	}
.top_view{
	width: 100%;
	height: 228rpx;
	padding: 0 30rpx;
	box-sizing: border-box;
	border-bottom: 1rpx solid #F4F4F4;
}
.top_view_item_img{
	width: 120rpx;
	height: 120rpx;
	display: block;
	margin: 0 auto;
}
.top_view_item_text{
	color: #666;
	text-align: center;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 26rpx;
	font-weight: 400;
	margin-top: 8rpx;
}
.tap_active{
	color: #333;
}
.list{
	padding: 0 30rpx;
	box-sizing: border-box;
}
.list_item_time{
	color: #999;
	text-align: center;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 24rpx;
	font-weight: 400;
	margin: 30rpx 0;
}
.list_item_content{
	padding:30rpx;
	box-sizing: border-box;
	border-radius: 20rpx;
	background: linear-gradient(180deg, #EFF5FD 0%, #FFF 100%);
}
.list_item_content_top{
	color: #333;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 32rpx;
	font-weight: 400;
}
.list_item_content_min{
	color: #8F9CAA;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 26rpx;
	font-weight: 400;
	margin-top: 20rpx;
}
.messg_icon{
	width: 32rpx;
	height: 32rpx;
}
.flex1name{
	color: #5293FE;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 24rpx;
	font-weight: 400;
}
.list_item_content_bottom{
	margin-top: 20rpx;
}
.epty{
	width: 100%;
	height: 100%;
	
}
.epty_zhanwei{
	width: 100%;
	height: 206rpx;
}
.epty_img{
	width: 270rpx;
	height: 136rpx;
	display: block;
	margin: 0 auto;
}
.epty_text{
	color: #333;
	text-align: center;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 30rpx;
	font-weight: 400;
	margin-top: 60rpx;
}
.list_ys{
	padding: 0 30rpx;
	box-sizing: border-box;
}
.list_ys_item_left{
	width: 96rpx;
	height: 96rpx;
	position: relative;
}
.list_ys_item_img{
	width: 96rpx;
	height: 96rpx;
	border-radius: 50%;
	background: #ccc;
}
.num{
	width: 36rpx;
	height: 36rpx;
	border-radius: 50%;
	border: 2rpx solid #fff;
	background: #F32B2B;
	color: #FFF;
	font-size: 26rpx;
	font-weight: 500;
	text-align: center;
	line-height: 36rpx;
	position: absolute;
	top: 0;
	right:0;
}
.list_ys_item_right{
	width: 570rpx;
	padding: 32rpx 0;
	box-sizing: border-box;
	border-bottom: 1rpx solid #F4F4F4;
}
.list_ys_item_right_top{
	color: #333;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 30rpx;
	font-weight: 400;
}
.list_ys_item_right_bottom{
	width: 486rpx;
	color: #999;
	font-family: Alibaba PuHuiTi 2.0;
	font-size: 26rpx;
	font-weight: 400;
	margin-top: 10rpx;
	
}
</style>
